# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
from urllib.parse import urlparse
from fire_control_spider.config import DEBUG


def update_media_urls(item, media_urls):
    """更新媒体URLs"""
    if media_urls:
        item['images'].extend(media_urls.get('images', []))
        item['videos'].extend(media_urls.get('videos', []))
        item['audios'].extend(media_urls.get('audios', []))
        item['main_files'].extend(media_urls.get('main_files', []))
        item['attachment_files'].extend(media_urls.get('attachment_files', []))

def extract_current_page_menu(response):
    """
    根据当前页面路径提取菜单层级信息
    
    Args:
        response: Scrapy响应对象
    
    Returns:
        list: 菜单层级数组，如 ['cate', 'books', 'animate']
    """
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 获取当前页面路径
    current_url = response.url
    parsed_url = urlparse(current_url)
    current_path = parsed_url.path
    
    
    # 标准化路径（确保以/结尾，除非是根路径）
    if current_path and current_path != '/' and not current_path.endswith('/'):
        current_path = current_path + '/'
    
    # 查找导航菜单
    navbar = soup.select_one('ul.navbar-nav')
    if not navbar:
        return []
    
    
    # 遍历所有导航链接
    nav_links = navbar.select('a')
    menu_hierarchy = []
    
    
    # 首先尝试精确匹配
    for link in nav_links:
        href = link.get('href', '')
        if not href:
            continue
            
        # 标准化href路径
        href_path = urlparse(href).path
        if href_path and href_path != '/' and not href_path.endswith('/'):
            href_path = href_path + '/'

        
        # 检查是否匹配当前页面
        if current_path == href_path:
            # 找到当前页面，构建菜单层级
            parent_li = link.find_parent('li')
            if parent_li:
                # 获取当前菜单项
                current_menu = link.get_text(strip=True)
                menu_hierarchy.append(current_menu)
                
                # 查找父级菜单
                parent_container = parent_li.find_parent('li')
                if parent_container:
                    parent_link = parent_container.find('a')
                    if parent_link:
                        parent_menu = parent_link.get_text(strip=True)
                        menu_hierarchy.insert(0, parent_menu)
                
                break

    
    
    return ['Home'] + menu_hierarchy

def get_iaff_headers():
    """获取IAFF网站专用的请求头"""
    return {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language': 'en-US,en;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-User': '?1',
        'Cache-Control': 'max-age=0',
        'DNT': '1',
    }